package com.thlws.study.redismq.internal;

import com.thlws.study.redismq.internal.queue.QueueSelector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author HanleyTang 2019-07-31
 */
@Component
public class MessageSender {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private final StringRedisTemplate stringRedisTemplate;

    @Resource(name = "myRedisTemplate")
    private RedisTemplate redisTemplate;

    public MessageSender(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    public void sendMessage(){
        stringRedisTemplate.convertAndSend("cctv",String.valueOf(Math.random()));
    }

    public void sendMessage(String channel, Object message){
        stringRedisTemplate.convertAndSend(channel,message);
    }


    @SuppressWarnings("unchecked")
    public void pushMessage(String ... message){
        String queueName = QueueSelector.getInstance().selectQueueName();
        redisTemplate.opsForList().leftPushAll(queueName,message);
    }

    @SuppressWarnings("unchecked")
    public void pushMessage(String message){
        String queueName = QueueSelector.getInstance().selectQueueName();
        redisTemplate.opsForList().leftPush(queueName,message);
    }

}
